package com.hefeng.wx.controller;

import com.hefeng.common.core.web.domain.AjaxResult;
import com.hefeng.wx.domain.WxUser;
import com.hefeng.wx.service.WxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hefeng.common.core.utils.EmptyUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/resident/user")
@Api(tags="居民端用户接口")
public class WxUserController {
    @Autowired
    WxUserService wxUserService;

    /**
    * 方法说明: 修改用户信息
    * @Param:
    * @return:
    * @Author: huzh
    * @Date: 2022/9/2
    */
    @RequestMapping(value = "/updateUser",produces = "application/json;charset=UTF-8")
    @ApiOperation("修改用户信息")
    public AjaxResult updateUser(@RequestBody WxUser wxUser){
        Map<String,Object> dataMap=new HashMap<>();
        wxUser.setUpdatedTime(new Date());
        wxUserService.updateById(wxUser);
        WxUser user=wxUserService.getById(wxUser.getUserId());
        dataMap.put("user",user);
        return AjaxResult.success("修改成功",dataMap);
    }

    /**
     * 方法说明: 注册用户信息
     * @Param: [userLogin]
     * @return: io.renren.util.ResultUtil
     * @Author: huzh
     * @Date: 2022/2/16
     */
    @RequestMapping(value = "/register",produces = "application/json;charset=UTF-8")
    @ApiOperation("注册")
    public AjaxResult register(@RequestBody WxUser wxUser){
        Map<String,Object> data=new HashMap<String,Object>();
        //查找是否是新用户
        WxUser user=wxUserService.getWxuserById(wxUser.getWxOpenId());
        if(EmptyUtils.isNotEmpty(user)){
            return AjaxResult.error(501,"该用户已注册！");
        }
        data=wxUserService.saveResident(wxUser);
        return AjaxResult.success("注册成功",data);
    }


}
